package me.seawenc.datastash.inputs;

import java.util.Properties;
import java.util.function.Consumer;

public abstract class AbstractProducer {
    protected Properties props;
    String filterInclude="";
    String filterExclude="";
    public AbstractProducer(Properties props){
        this.props=props;
        filterInclude=props.getProperty("filter.include");
        filterExclude=props.getProperty("filter.exclude");
    }

    public void send(Consumer<String> consumer,String msg){
        if(filter(msg)){
            consumer.accept(msg);
        }
    }

    /**
     *  数据过滤
     * @param msg
     */
    public boolean filter(String msg){
        if(filterInclude!=null && !"".equals(filterInclude.trim())){
            if(!msg.contains(filterInclude)){
                return false;
            }
        }
        if(filterExclude!=null && !"".equals(filterExclude.trim())){
            if(msg.contains(filterExclude)){
                return false;
            }
        }
        return true;
    }

    public abstract void producer(Consumer<String> consumer) throws Exception;
}
